package logic

import (
	"context"
	"errors"
	"net/http"

	"bigmodel/internal/svc"
	"bigmodel/internal/types"
	"bigmodel/model"
	"bigmodel/service/chat"

	"github.com/zeromicro/go-zero/core/logx"
)

type BigmodelLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
	ptmodel model.PlatformModel
}

func NewBigmodelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BigmodelLogic {
	return &BigmodelLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
		ptmodel: model.NewPlatformModel(svcCtx.Orm),
	}
}

func (l *BigmodelLogic) Bigmodel(req *types.Request, w http.ResponseWriter) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	client, err := chat.GetResource(l.ctx, req.Platform)
	if err != nil {
		return nil, errors.New("获取资源错误")
	}
	args := &chat.ChatDoArgsV1{
		Model: req.Model,
		Content: req.Content,
	}
	if req.Stream == "0" {
		args.Stream = false
	}else{
		args.Stream = true
	}
	if req.Platform == "spark"{
		args.Stream = true	
	}

	pt,err := l.ptmodel.GetOneByName(l.ctx,req.Platform)
	if err != nil {
		return nil,errors.New(err.Error())
	}

	res, err := client.Chat(l.ctx, w, args,pt.InputConf)
	if err != nil {
		return nil, errors.New("出错咯")
	}
	defer chat.ReleaseResource(l.ctx, req.Platform, client)
	return &types.Response{
		Message: res.Message,
	}, nil
}
